package com.isoft.controller.blls;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.isoft.bean.ResponseData;
import com.isoft.controller.BaseController;
import com.isoft.entity.ball.Blls;
import com.isoft.entity.flight.FlightInfo;
import com.isoft.service.blls.impl.BllsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 行李
 */
@RestController
@RequestMapping("/blls")
@CrossOrigin
public class BllsController extends BaseController {
    @Autowired
    BllsServiceImpl bllsService;
    @GetMapping("/all")
    public ResponseData<Object> returnObject() {
        List<Blls> blls = bllsService.findAllBlls();
        return returnObject(blls);
    }

    @GetMapping("/findBy/{META_ID}")
    public ResponseData<Object> returnObject(@PathVariable Integer META_ID) {
        if (META_ID == null) {
            return returnObject(null, "400", "航班号不能为空");
        }
        List<Blls> blls = bllsService.findBllsBySEQN(META_ID);
        if (blls.isEmpty()){
            return returnObject(null, "400", "航班号不存在");
        }
        return returnObject(blls);
    }
    @GetMapping("/page")
    public ResponseData<List<Blls>> getBllsInfosPage(
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "5") int pageSize) {
        try {
            // 调用服务层获取分页数据
            IPage<Blls> bllsPage = bllsService.getBllsInfosPage(currentPage, pageSize);

            // 从IPage中提取数据列表和总记录数
            List<Blls> dataList = bllsPage.getRecords(); // 获取数据列表
            long total = bllsPage.getTotal(); // 获取总记录数

            // 封装到ResponseData中
            return ResponseData.success(dataList, total);
        } catch (Exception e) {
            // 异常处理，返回错误信息
            return ResponseData.failure("500", "获取Blls信息失败: " + e.getMessage());
        }
    }
//    添加
    @PostMapping("/insert")
    public ResponseData<Boolean> insertBlls(@RequestBody Blls blls) {
        if (blls == null) {
            return returnBoolean(false, "400", "数据不能为空");
        }
        Boolean result = bllsService.insertBlls(blls);
        if(result){
            return returnBoolean(result, "200", "添加成功");
        }else {
            return returnBoolean(result, "500", "添加失败！该航班号信息已存在");
        }
    }
//    修改
    @PutMapping("/update")
    public ResponseData<Boolean> updateBlls(@RequestBody Blls blls) {
        if (blls == null) {
            return returnBoolean(false, "400", "数据不能为空");
        }
        Boolean result = bllsService.updateBlls(blls);
        return returnBoolean(result);
    }
//    删除
    @DeleteMapping("/{ID}")
    public ResponseData<Boolean> deleteBlls(@PathVariable Integer ID) {
        if (ID == null) {
            return returnBoolean(false, "400", "ID不能为空");
        }
        Boolean result = bllsService.deleteBlls(ID);
        return returnBoolean(result);
    }
}
